﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;

namespace PipePrinter
{
    public partial class frmSetting : Form
    {
        public frmSetting()
        {
            InitializeComponent();

            string[] portNames = SerialPort.GetPortNames();
            Array.Sort(portNames);

            foreach (string s in portNames)
            {
                cbiPortName.Items.Add(s);
                cboPortName.Items.Add(s);
            }

            foreach (string s in Enum.GetNames(typeof(Handshake)))
            {
                cbiHandshake.Items.Add(s);
                cboHandshake.Items.Add(s);
            }

            foreach (string s in Enum.GetNames(typeof(Parity)))
            {
                cbiParity.Items.Add(s);
                cboParity.Items.Add(s);
            }

            foreach (string s in Enum.GetNames(typeof(StopBits)))
            {
                cbiStopBits.Items.Add(s);
                cboStopBits.Items.Add(s);
            }

            ComPortConfig config = SysConfig.getInstance().InputPort;
            cbiPortName.Text = config.PortName;
            cbiBaudRate.Text = config.BaudRate;
            cbiDataBits.Text = config.DataBits;
            cbiStopBits.Text = config.StopBits;
            cbiParity.Text = config.Parity;
            cbiHandshake.Text = config.Handshake;

            config = SysConfig.getInstance().OutputPort;
            cboPortName.Text = config.PortName;
            cboBaudRate.Text = config.BaudRate;
            cboDataBits.Text = config.DataBits;
            cboStopBits.Text = config.StopBits;
            cboParity.Text = config.Parity;
            cboHandshake.Text = config.Handshake;

            DelayConfig dconfig = SysConfig.getInstance().Delay;
            cbCount.Text = dconfig.CountValue.ToString();
            cbTime.Text = dconfig.DelayMsValue;
            if (dconfig.Type == DelayType.ByCount)
                rbByCount.Checked = true;
            else
                rbByTime.Checked = true;

        }

        private void SaveConfig()
        {
            ComPortConfig config = SysConfig.getInstance().InputPort;
            config.PortName = cbiPortName.Text;
            config.BaudRate = cbiBaudRate.Text;
            config.DataBits = cbiDataBits.Text;
            config.StopBits = cbiStopBits.Text;
            config.Parity = cbiParity.Text;
            config.Handshake = cbiHandshake.Text;

            config = SysConfig.getInstance().OutputPort;
            config.PortName = cboPortName.Text;
            config.BaudRate = cboBaudRate.Text;
            config.DataBits = cboDataBits.Text;
            config.StopBits = cboStopBits.Text;
            config.Parity = cboParity.Text;
            config.Handshake = cboHandshake.Text;

            DelayConfig dconfig = SysConfig.getInstance().Delay;
            dconfig.CountValue = cbCount.Text;
            dconfig.DelayMsValue = cbTime.Text;

            if (rbByCount.Checked)
                dconfig.Type = DelayType.ByCount;
            else
                dconfig.Type = DelayType.ByTime;

            SysConfig.getInstance().Save();

        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            SaveConfig();
            Close();
        }
    }
}
